<?php
// @formatter:off
/**
 * @file c.directory.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: c.directory.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/themes/default/controlers/contents/c.directory.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_DIRECTORY_VIEW)) {
	require_once "{$this->_themeRoot}/controlers/generics/comicpreview.php";
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wctDefaults;

	if($wcDirectory && $wcDirectory->ok()) {
		$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
		$minCmcInPage = $wctDefaults["max-per-page"]["directory"] * ($page - 1);
		$maxCmcInPage = $minCmcInPage + $wctDefaults["max-per-page"]["directory"];

		$wcThemeAssigns["PATH"] = "{$wcDirectory->name}";
		$wcThemeAssigns['ACTIONCONTENT'] = "/contents/c.directory.html";
		$wcThemeAssigns['ACTIONPRESCRIPT'] = "/contents/c.directory.js";

		$wcThemeAssigns["INFO-SUBDIRECTORIES"] = array();

		$has = false;
		foreach($wcDirectory->subDirectories() as $directory) {
			$has = true;
			$wcThemeAssigns["INFO-SUBDIRECTORIES"][] = array(
				"id" => $directory->id,
				"name" => $directory->name
			);
		}
		if(!$has) {
			unset($wcThemeAssigns["INFO-SUBDIRECTORIES"]);
		}

		$parent = $wcDirectory->parentDirectory();
		if($parent) {
			$wcThemeAssigns["INFO-PARENT-ID"] = $parent->id;
			$wcThemeAssigns["INFO-PARENT-NAME"] = $parent->name;
		}

		if(isset($wcThemeAssigns["INFO-PARENT-ID"]) || isset($wcThemeAssigns["INFO-SUBDIRECTORIES"])) {
			$wcThemeAssigns["INFO-MOREINFO"] = true;
		}

		$wcThemeAssigns["comics"] = array();
		$comicCount = 0;
		foreach($wcDirectory->membersFull() as $comic) {
			if($comic->ok()) {
				if($comicCount >= $minCmcInPage && $comicCount < $maxCmcInPage) {
					$wcThemeAssigns["comics"][] = wcGetComicPreviewAssigns($comic);
				}
				$comicCount++;
			}
		}

		wcSetPager(requestParams(true, $wcAction, array('page')), ceil($comicCount / $wctDefaults["max-per-page"]["directory"]), $page);

		if($wcDirectory->ok() && !$wcDirectory->valid_preview) {
			$wcDirectory->generatePreviews();
		}

		if($wcProfile->allowedTo(WC_PERM_DIRECTORY_RENAME)) {
			$wcThemeAssigns["INFO-NAME-EDITABLE"] = true;
		}
	} else {
		$wcThemeAssigns["error-string"] = "Invalid directory";
		require "{$this->_themeRoot}/controlers/contents/c.500.php";
	}
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to open directories";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>
